/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * Panel.java
 *
 * Created on Apr 24, 2011, 6:25:44 PM
 */
package POS_Source;

import DTO.Item;
import DTO.Order;
import POS_Controller.POSController;
import Utilities.MoneyFormatter;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.print.PageFormat;
import java.awt.print.Printable;
import java.awt.print.PrinterException;
import java.awt.print.PrinterJob;
import java.util.ListIterator;
import java.util.Vector;
import javax.swing.table.DefaultTableModel;
import javax.swing.table.TableColumn;

/**
 * this class renders print receipt view 
 * @author Claudia
 * @see POS_Source
 */
public class POS_PrintReceipt extends javax.swing.JPanel implements Printable {

    private POSController controller;
    private Order order;
    private static final String[] columnNames = new String[]{"UPC", "Name", "Quantity", "Price"};
    private MoneyFormatter moneyFormatter;

    /**
     * constructor for this class
     * @param control
     */
    public POS_PrintReceipt(POSController control) {
        initComponents();
        controller = control;
        order = controller.getSessionManager().getOrder();
        moneyFormatter = new MoneyFormatter();
        customInit();
    }
/**
 * this method puts a new job in printing queue
 * @throws RuntimeException
 */
    public void printOrder() throws RuntimeException {
        PrinterJob printJob = PrinterJob.getPrinterJob();
        printJob.setPrintable(POS_PrintReceipt.this);
        if (printJob.printDialog()) {
            try {
                printJob.print();
            } catch (Exception ex) {
                throw new RuntimeException(ex);
            }
        }
    }
/**
 * this method overrides default view rendering
 */
    public final void customInit() {
        initTable();
        setTotals();
        OrderNumberLabel.setText(Integer.toString(order.getID()));
    }
/**
 * this method renders the cart table summary
 */
    private void initTable() {

        ListIterator<Item> iter = order.getItemList().listIterator();
        DefaultTableModel model = new DefaultTableModel(columnNames, 0);

        while (iter.hasNext()) {
            Item item = iter.next();
            Vector row = new Vector();
            row.add(item.getUpc());
            row.add(item.getName());
            row.add(item.getQty());
            row.add(moneyFormatter.format(item.getSoldPrice() * item.getQty()));
            model.addRow(row);
            if (item.getDiscountAmount() > 0.0 && controller.getSessionManager().getLoggedIn()) {
                row = new Vector();
                row.add("");
                row.add("Discount");
                row.add(item.getQty());
                row.add("-" + moneyFormatter.format(item.getDiscountAmount() * item.getQty()));
                model.addRow(row);
            }
        }

        ResultTable.setModel(model);

        TableColumn column = null;
        column = ResultTable.getColumnModel().getColumn(0);
        column.setPreferredWidth(100); //third column is bigger
        column = ResultTable.getColumnModel().getColumn(1);
        column.setPreferredWidth(300);
        column = ResultTable.getColumnModel().getColumn(2);
        column.setPreferredWidth(25);
        column = ResultTable.getColumnModel().getColumn(3);
        column.setPreferredWidth(50);

        ResultTable.setRowHeight(40);

        ResultTable.setColumnSelectionAllowed(false);
        ResultTable.setRowSelectionAllowed(false);
    }
/**
 * this method updates dynamic receipt 
 */
    private void setTotals() {
        DiscountAmount.setText(moneyFormatter.format(order.getTotalItemDiscount()));
        SubtotalAmount.setText(moneyFormatter.format(order.getItemSubTotal()));
        TaxAmount.setText(moneyFormatter.format(order.getTaxAmount()));
        TotalAmount.setText(moneyFormatter.format(order.getGrandTotal()));
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        TitlePanel = new javax.swing.JPanel();
        jPanel1 = new javax.swing.JPanel();
        TotalAmount = new javax.swing.JLabel();
        SubTotalLabel1 = new javax.swing.JLabel();
        DiscountAmount = new javax.swing.JLabel();
        ResultPane = new javax.swing.JScrollPane();
        ResultTable = new javax.swing.JTable();
        TaxAmount = new javax.swing.JLabel();
        TaxLabel = new javax.swing.JLabel();
        SubTotalLabel = new javax.swing.JLabel();
        TotalLabel = new javax.swing.JLabel();
        SubtotalAmount = new javax.swing.JLabel();
        LogoLabel = new javax.swing.JLabel();
        jLabel3 = new javax.swing.JLabel();
        jLabel4 = new javax.swing.JLabel();
        jLabel5 = new javax.swing.JLabel();
        OrderNumberLabel = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();
        jButton1 = new javax.swing.JButton();
        jLabel7 = new javax.swing.JLabel();
        jLabel8 = new javax.swing.JLabel();
        TitleLabel = new javax.swing.JLabel();

        setBackground(new java.awt.Color(255, 255, 255));

        TitlePanel.setBackground(new java.awt.Color(255, 255, 255));

        javax.swing.GroupLayout TitlePanelLayout = new javax.swing.GroupLayout(TitlePanel);
        TitlePanel.setLayout(TitlePanelLayout);
        TitlePanelLayout.setHorizontalGroup(
            TitlePanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 26, Short.MAX_VALUE)
        );
        TitlePanelLayout.setVerticalGroup(
            TitlePanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 90, Short.MAX_VALUE)
        );

        jPanel1.setBackground(new java.awt.Color(255, 255, 255));

        TotalAmount.setFont(new java.awt.Font("Tahoma", 0, 24)); // NOI18N
        TotalAmount.setText("$00000.00");

        SubTotalLabel1.setFont(new java.awt.Font("Tahoma", 0, 18)); // NOI18N
        SubTotalLabel1.setText("Discount:");

        DiscountAmount.setFont(new java.awt.Font("Tahoma", 0, 18)); // NOI18N
        DiscountAmount.setText("$00000.00");

        ResultTable.setFont(new java.awt.Font("Tahoma", 0, 14)); // NOI18N
        ResultTable.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null}
            },
            new String [] {
                "UPC", "Name", "Qty", "Total"
            }
        ));
        ResultPane.setViewportView(ResultTable);
        ResultTable.getColumnModel().getColumn(0).setMinWidth(130);
        ResultTable.getColumnModel().getColumn(0).setMaxWidth(130);
        ResultTable.getColumnModel().getColumn(1).setMinWidth(220);
        ResultTable.getColumnModel().getColumn(1).setMaxWidth(221);
        ResultTable.getColumnModel().getColumn(2).setMinWidth(88);
        ResultTable.getColumnModel().getColumn(2).setMaxWidth(89);
        ResultTable.getColumnModel().getColumn(3).setMinWidth(129);
        ResultTable.getColumnModel().getColumn(3).setMaxWidth(130);

        TaxAmount.setFont(new java.awt.Font("Tahoma", 0, 18)); // NOI18N
        TaxAmount.setText("$00000.00");

        TaxLabel.setFont(new java.awt.Font("Tahoma", 0, 18)); // NOI18N
        TaxLabel.setText("Tax:");

        SubTotalLabel.setFont(new java.awt.Font("Tahoma", 0, 18));
        SubTotalLabel.setText("Subtotal:");

        TotalLabel.setFont(new java.awt.Font("Tahoma", 0, 24)); // NOI18N
        TotalLabel.setText("Total:");

        SubtotalAmount.setFont(new java.awt.Font("Tahoma", 0, 18)); // NOI18N
        SubtotalAmount.setText("$00000.00");

        LogoLabel.setIcon(new javax.swing.ImageIcon(getClass().getResource("/POS_Source/images/Logo.png"))); // NOI18N

        jLabel3.setFont(new java.awt.Font("Tahoma", 2, 24)); // NOI18N
        jLabel3.setText("Thank you for shopping");

        jLabel4.setFont(new java.awt.Font("Tahoma", 2, 24)); // NOI18N
        jLabel4.setText("at Digistore");

        jLabel5.setFont(new java.awt.Font("Tahoma", 2, 24)); // NOI18N
        jLabel5.setText("Order Number:");

        OrderNumberLabel.setFont(new java.awt.Font("Tahoma", 2, 24)); // NOI18N
        OrderNumberLabel.setText("888888");

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addComponent(ResultPane, javax.swing.GroupLayout.DEFAULT_SIZE, 586, Short.MAX_VALUE)
                        .addContainerGap())
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addComponent(LogoLabel, javax.swing.GroupLayout.PREFERRED_SIZE, 272, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 47, Short.MAX_VALUE)
                        .addComponent(jLabel5, javax.swing.GroupLayout.PREFERRED_SIZE, 170, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(OrderNumberLabel, javax.swing.GroupLayout.PREFERRED_SIZE, 91, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addContainerGap())
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.CENTER)
                            .addComponent(jLabel4, javax.swing.GroupLayout.PREFERRED_SIZE, 135, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(jLabel3, javax.swing.GroupLayout.PREFERRED_SIZE, 261, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addGap(102, 102, 102)
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(SubTotalLabel1, javax.swing.GroupLayout.Alignment.TRAILING)
                            .addGroup(jPanel1Layout.createSequentialGroup()
                                .addGap(3, 3, 3)
                                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                    .addComponent(TaxLabel, javax.swing.GroupLayout.Alignment.TRAILING)
                                    .addComponent(TotalLabel, javax.swing.GroupLayout.Alignment.TRAILING)
                                    .addComponent(SubTotalLabel, javax.swing.GroupLayout.Alignment.TRAILING))))
                        .addGap(3, 3, 3)
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(TaxAmount, javax.swing.GroupLayout.DEFAULT_SIZE, 121, Short.MAX_VALUE)
                            .addComponent(SubtotalAmount, javax.swing.GroupLayout.DEFAULT_SIZE, 121, Short.MAX_VALUE)
                            .addComponent(TotalAmount, javax.swing.GroupLayout.DEFAULT_SIZE, 121, Short.MAX_VALUE)
                            .addComponent(DiscountAmount, javax.swing.GroupLayout.DEFAULT_SIZE, 121, Short.MAX_VALUE))
                        .addGap(35, 35, 35))))
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(LogoLabel)
                    .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                        .addComponent(jLabel5)
                        .addComponent(OrderNumberLabel)))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(ResultPane, javax.swing.GroupLayout.PREFERRED_SIZE, 398, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addComponent(jLabel3)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jLabel4))
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(SubTotalLabel1)
                            .addComponent(DiscountAmount))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(SubTotalLabel)
                            .addComponent(SubtotalAmount))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(TaxLabel)
                            .addComponent(TaxAmount))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(TotalAmount)
                            .addComponent(TotalLabel))))
                .addContainerGap())
        );

        jLabel2.setFont(new java.awt.Font("Tahoma", 2, 24)); // NOI18N
        jLabel2.setText("Your order has been processed!");

        jButton1.setFont(new java.awt.Font("Tahoma", 0, 24)); // NOI18N
        jButton1.setText("Print");
        jButton1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton1ActionPerformed(evt);
            }
        });

        jLabel7.setFont(new java.awt.Font("Tahoma", 2, 24));
        jLabel7.setText("receipt to front desk.");

        jLabel8.setFont(new java.awt.Font("Tahoma", 2, 24));
        jLabel8.setText("Please print and take your");

        TitleLabel.setFont(new java.awt.Font("Copperplate Gothic Bold", 2, 60)); // NOI18N
        TitleLabel.setText("Order Confirmed");

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(20, 20, 20)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.CENTER)
                            .addComponent(jLabel7, javax.swing.GroupLayout.PREFERRED_SIZE, 230, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(jLabel2, javax.swing.GroupLayout.PREFERRED_SIZE, 354, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(jLabel8, javax.swing.GroupLayout.PREFERRED_SIZE, 299, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(jButton1, javax.swing.GroupLayout.PREFERRED_SIZE, 139, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(11, 11, 11))
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(TitleLabel, javax.swing.GroupLayout.PREFERRED_SIZE, 606, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(TitlePanel, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                        .addGap(883, 883, 883))))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addGap(129, 129, 129)
                        .addComponent(jLabel2)
                        .addGap(54, 54, 54)
                        .addComponent(jLabel8)
                        .addGap(18, 18, 18)
                        .addComponent(jLabel7)
                        .addGap(121, 121, 121)
                        .addComponent(jButton1, javax.swing.GroupLayout.PREFERRED_SIZE, 84, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(layout.createSequentialGroup()
                        .addContainerGap()
                        .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addGap(37, 37, 37)
                        .addComponent(TitlePanel, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(layout.createSequentialGroup()
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(TitleLabel)))
                .addContainerGap())
        );
    }// </editor-fold>//GEN-END:initComponents
/**
 * this method handles button press event 
 * @param evt
 */
    private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton1ActionPerformed
        // TODO add your handling code here:
        printOrder();
        controller.newSession();
    }//GEN-LAST:event_jButton1ActionPerformed
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JLabel DiscountAmount;
    private javax.swing.JLabel LogoLabel;
    private javax.swing.JLabel OrderNumberLabel;
    private javax.swing.JScrollPane ResultPane;
    private javax.swing.JTable ResultTable;
    private javax.swing.JLabel SubTotalLabel;
    private javax.swing.JLabel SubTotalLabel1;
    private javax.swing.JLabel SubtotalAmount;
    private javax.swing.JLabel TaxAmount;
    private javax.swing.JLabel TaxLabel;
    private javax.swing.JLabel TitleLabel;
    private javax.swing.JPanel TitlePanel;
    private javax.swing.JLabel TotalAmount;
    private javax.swing.JLabel TotalLabel;
    private javax.swing.JButton jButton1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JLabel jLabel7;
    private javax.swing.JLabel jLabel8;
    private javax.swing.JPanel jPanel1;
    // End of variables declaration//GEN-END:variables

    /**
     *
     * @param g
     * @param pf
     * @param index
     * @return
     * @throws PrinterException
     */
    public int print(Graphics g, PageFormat pf, int index) throws
            PrinterException {

        Graphics2D g2 = (Graphics2D) g;
        if (index >= 1) {
            return Printable.NO_SUCH_PAGE;
        } else {

            jPanel1.printAll(g2);
            return Printable.PAGE_EXISTS;
        }
    }
}
